﻿using Bookify.Domain.Users;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bookify.Infrastructure.Repositories
{
    internal sealed class UserRespository : Repository<User>, IUserRepository
    {
        public UserRespository(ApplicationDbContext dbContext) : base(dbContext)
        {
        }

        public async Task<User?> GetByIdentityIdAsync(Guid identityId, CancellationToken cancellationToken = default)
        {
            return await DbContext
                .Set<User>()
                .FirstOrDefaultAsync(x => x.IdentityId == identityId.ToString(), cancellationToken);
        }

        public override void Add(User user)
        {
            foreach (var role in user.Roles)
            {
                DbContext.Attach(role);
            }

            DbContext.Add(user);
        }
    }
}
